//
//  XTXXiaoxiViewController.m
//  xiaotianxin
//
//  Created by XYC on 2019/6/11.
//  Copyright © 2019 XYC. All rights reserved.
//

#import "XTXXiaoxiViewController.h"
#import "XTXChatModel.h"
#import "XTXXiaoxiCell.h"
#import "XTXXitongViewController.h"
#import "XTXHudongViewController.h"
#import "XTXChatViewController.h"

@interface XTXXiaoxiViewController ()
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, strong) NSMutableArray *xiaoxiArr;

@end

@implementation XTXXiaoxiViewController


- (NSMutableArray *)xiaoxiArr
{
    if (!_xiaoxiArr) {
        _xiaoxiArr = [NSMutableArray array];
    }
    return _xiaoxiArr;
}

- (void)tianjiatubiaoClick
{
    if (self.xiaoxiArr.count == 0) {
        XTXChatModel *mesageMod1 = [[XTXChatModel alloc] init];
        mesageMod1.nickname = @"系统通知";
        mesageMod1.headimage = @"互动通知";
        [self.xiaoxiArr addObject:mesageMod1];
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.xiaoxiArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XTXXiaoxiCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(XTXXiaoxiCell.class) forIndexPath:indexPath];
    XTXChatModel *quartpremises = [[XTXChatModel alloc] init];
    if (self.xiaoxiArr.count > indexPath.row) {
        quartpremises = self.xiaoxiArr[indexPath.row];
    }
    if (indexPath.row == 0) {
        cell.xingbieImageView.hidden = YES;
        cell.beijingImageView.hidden = YES;
    }
    else if (indexPath.row == 1) {
        cell.xingbieImageView.hidden = YES;
        cell.beijingImageView.hidden = YES;
    }
    else {
        cell.xingbieImageView.hidden = NO;
        if (quartpremises.goddessflag.integerValue) {
            cell.beijingImageView.hidden = NO;
        }
        else {
            cell.beijingImageView.hidden = YES;
        }
    }
    cell.chatModel = quartpremises;
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != 0 && indexPath.row != 1 && editingStyle == UITableViewCellEditingStyleDelete) {
        XTXChatModel *model = [[XTXChatModel alloc] init];
        model = self.xiaoxiArr[indexPath.row];
        [self shanchuxiaoxidewangluoqingqiu:model.friend_user_id withIndex:indexPath.row];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}

- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row==0) {
//        XTXXitongViewController *msgViewController = [[XTXXitongViewController alloc] init];
//        msgViewController.hidesBottomBarWhenPushed=YES;
//        [self.navigationController pushViewController:msgViewController animated:YES];
//    }
//    else
        if (indexPath.row==0) {
        XTXHudongViewController *sysmViewControoler = [[XTXHudongViewController alloc] init];
        sysmViewControoler.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:sysmViewControoler animated:YES];
    }
    else{
        if (self.xiaoxiArr.count > indexPath.row) {
            XTXChatViewController *msgViewController = [[XTXChatViewController alloc] initWithNibName:NSStringFromClass(XTXChatViewController.class) bundle:nil];
            msgViewController.mesageMod = self.xiaoxiArr[indexPath.row];
            msgViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:msgViewController animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72;
}




- (void)huoquxiaoxiliebiaodewangluoqingqiuClick
{
    
    NSMutableDictionary *canshuMutDict = [NSMutableDictionary dictionary];
    canshuMutDict[@"user_id"] = XTXGongTool.share.useMode.id;
    canshuMutDict[@"page"] = [NSString stringWithFormat:@"%ld", self.pageCount];
    WeakSelf
    [XTXNetworking networkingGetUseMsgdewangluoqingqiu:canshuMutDict.copy completionHandler:^(NSDictionary * _Nonnull dict) {
        if ([dict[@"code"] integerValue] == 1) {
            if (weakSelf.pageCount == 1) {
                [weakSelf.xiaoxiArr removeAllObjects];
                [weakSelf tianjiatubiaoClick];
            }
            for (NSDictionary *dataDictionary in dict[@"info"]) {
                XTXChatModel *mesageMod = [XTXChatModel yy_modelWithDictionary:dataDictionary];
                [weakSelf.xiaoxiArr addObject:mesageMod];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (weakSelf.tableView.mj_header.isRefreshing) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            if (weakSelf.tableView.mj_footer.isRefreshing) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
        });
    }];
}

- (void)shanchuxiaoxidewangluoqingqiu:(NSString *) friend_user_id withIndex:(NSInteger) index
{
    NSMutableDictionary *canshuMutDict = [NSMutableDictionary dictionary];
    canshuMutDict[@"user_id"] = XTXGongTool.share.useMode.id;
    canshuMutDict[@"friend_user_id"] = friend_user_id;
    WeakSelf
    [XTXNetworking networkingDelUseMsgdewangluoqingqiu:canshuMutDict.copy completionHandler:^(NSDictionary * _Nonnull dict) {
        if ([dict[@"code"] integerValue] == 1) {
            [weakSelf.xiaoxiArr removeObjectAtIndex:index];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [XTXGongView showTipMsg:dict[@"msg"]];
                [weakSelf.tableView reloadData];
            });
        }
    }];
}




- (void)refreshData
{
    WeakSelf
    MJRefreshStateHeader *concreteRefreshHeader = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        weakSelf.pageCount = 1;
        [weakSelf huoquxiaoxiliebiaodewangluoqingqiuClick];
        
    }];
    [concreteRefreshHeader setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [concreteRefreshHeader setTitle:@"松开立即刷新" forState:MJRefreshStatePulling];
    [concreteRefreshHeader setTitle:@"正在刷新..." forState:MJRefreshStateRefreshing];
    [concreteRefreshHeader setTitle:@"刷新成功" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_header = concreteRefreshHeader;
    MJRefreshAutoStateFooter *concreteRefreshFooter = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pageCount ++;
        [weakSelf huoquxiaoxiliebiaodewangluoqingqiuClick];
    }];
    [concreteRefreshFooter setTitle:@"" forState:MJRefreshStateIdle];
    [concreteRefreshFooter setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    self.tableView.mj_footer = concreteRefreshFooter;
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.pageCount = 1;
    [self huoquxiaoxiliebiaodewangluoqingqiuClick];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = XZColor(245, 245, 245);
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 9999, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(XTXXiaoxiCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(XTXXiaoxiCell.class)];
    [self refreshData];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        UITabBarItem* tabItem = [[UITabBarItem alloc]init];
        tabItem.selectedImage =  [[UIImage imageNamed:@"简讯on" ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabItem.image = [[UIImage imageNamed:@"简讯off"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabItem.title = @"消息";
        self.title = tabItem.title;

        [tabItem setTitleTextAttributes:@{NSForegroundColorAttributeName:rgbYanse1(0x82e0ff)} forState:UIControlStateNormal];
        [tabItem setTitleTextAttributes:@{NSForegroundColorAttributeName:rgbYanse1(0x5974ff)} forState:UIControlStateSelected];
        self.tabBarItem = tabItem;
        
    }
    return self;
}

@end
